﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain.Commands;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Events;
using xiaodingmarket_Infra_Data.Repository.EventStore;

namespace xiaodingmarket_application.Service
{
    public class CommodityService : ICommodityService
    {
        private readonly IMapper _mapper;
        private readonly ICommodityRepository _commodityRepository;
        private readonly IEventStoreRepository _eventStoreRepository;
        private readonly IMediatorHandler Bus;

        public CommodityService(IMapper mapper,
                                  ICommodityRepository commodityRepository,
                                  IMediatorHandler bus,
                                  IEventStoreRepository eventStoreRepository)
        {
            _mapper = mapper;
            _commodityRepository = commodityRepository;
            Bus = bus;
            _eventStoreRepository = eventStoreRepository;
        }
        /// <summary>
        /// 发布商品
        /// </summary>
        /// <param name="commdityViewModel"></param>
        public void Create(CommodityViewModel commdityViewModel)
        {
            var createCommand = _mapper.Map<CreateNewCommodityCommand>(commdityViewModel);
            Bus.SendCommand(createCommand);
        }
        /// <summary>
        /// 用户更新商品
        /// </summary>
        /// <param name="commdityViewModel"></param>
        public void Update(CommodityViewModel commdityViewModel)
        {
            var updateCommand = _mapper.Map<UpdateCommodityCommand>(commdityViewModel);
            Bus.SendCommand(updateCommand);
        }
        /// <summary>
        /// 加载全部（大分类下）
        /// </summary>
        /// <param name="kind"></param>
        /// <returns></returns>
        public IEnumerable<CommodityViewModel> GetAll(int kind)
        {
            return _mapper.Map<IEnumerable<CommodityViewModel>>(_commodityRepository.GetAll(kind));
        }
        /// <summary>
        /// 返回商品详情，不是发布人的请求会浏览量++
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public CommodityViewModel GetById(Guid id,Guid userid)
        {
            var commdity = _commodityRepository.GetOneById(id);
            
            if (userid != commdity.CommodityDetail.StudentId)
            {
                commdity.ViewsCount++;
                _commodityRepository.Update(commdity);
            }
            return _mapper.Map<CommodityViewModel>(commdity);
        }

        public IEnumerable<CommodityViewModel> GetAnyByRemark(string remark)
        {
            return _mapper.Map<IEnumerable<CommodityViewModel>>(_commodityRepository.GetAnyByRemark(remark));
        }

        public IEnumerable<CommodityViewModel> GetAnyByCategory(int category)
        {
            return _mapper.Map<IEnumerable<CommodityViewModel>>(_commodityRepository.GetAnyByCategory(category));
        }
        public void Remove(Guid id)
        {
            var removeCommand = new RemoveCommodityCommand(id);
            Bus.SendCommand(removeCommand);
        }
        public void RemoveAll(Guid[] ids)
        {
            foreach(var id in ids)
            {
                var removeCommand = new RemoveCommodityCommand(id);
                Bus.SendCommand(removeCommand);
            }
            
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }


    }
}
